﻿using FMStudio.Documents.DataDocument.Properties;
using FMStudio.Domain.SQLite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace FMStudio.Documents.DataDocument.Test
{
    [TestClass]
    public class DataDocumentTypeTest
    {
        public string TestDataPath1 = Path.Combine(Environment.CurrentDirectory, "TestData1.data");
        public string TestDataPath2 = Path.Combine(Environment.CurrentDirectory, "TestData2.data");

        [TestInitialize]
        public void TestCleanup()
        {
            if (File.Exists(TestDataPath1))
                File.Delete(TestDataPath1);
            if (File.Exists(TestDataPath2))
                File.Delete(TestDataPath2);
        }

        [TestMethod]
        public void DocumentTypeTest()
        {
            DataDocumentType documentType = new DataDocumentType();
            Assert.AreEqual(Resources.DocumentExtension, documentType.FileExtension);
            Assert.AreEqual(Resources.DocumentDescription, documentType.Description);
        }

        [TestMethod]
        public void NewDocumentTest()
        {
            DataDocumentType documentType = new DataDocumentType();
            Assert.IsTrue(documentType.CanNew());

            DataDocument document = documentType.New(TestDataPath1);
            Assert.IsTrue(File.Exists(TestDataPath1));
            Assert.IsNotNull(document);

            Assert.IsNotNull(document.Entities);
            Assert.IsNotNull(document.AuditBills);
            Assert.IsNotNull(document.AuditTypes);
            Assert.IsNotNull(document.Bills);
            Assert.IsNotNull(document.Cities);
            Assert.IsNotNull(document.Currencies);
            Assert.IsNotNull(document.CurrencyExchanges);
            Assert.IsNotNull(document.Items);
            Assert.IsNotNull(document.ItemPrices);
            Assert.IsNotNull(document.Markets);
            Assert.IsNotNull(document.PayTypes);
            Assert.IsNotNull(document.PayTypeCategories);
            Assert.IsNotNull(document.Trademarks);
            Assert.IsNotNull(document.UseTypes);
            Assert.IsNotNull(document.UseTypeCategories);
        }

        [TestMethod]
        public void SaveAndOpenDocumentTest()
        {
            DataDocumentType documentType = new DataDocumentType();
            DataDocument document = documentType.New(TestDataPath1);

            Assert.IsTrue(documentType.CanSave(document));
            documentType.Save(document, TestDataPath1);
            Assert.IsTrue(File.Exists(TestDataPath1));

            Assert.IsTrue(documentType.CanOpen());
            DataDocument openedDocument = documentType.Open(TestDataPath1);

            Assert.IsNotNull(openedDocument.Entities);

            //  Modify the Data
            openedDocument = null;
            document.Cities.Add(new City() { Name = "Shanghai" });
            Assert.AreEqual(1, document.Cities.Count);
            documentType.Save(document, TestDataPath1);
            openedDocument = documentType.Open(TestDataPath1);
            Assert.AreEqual(1, openedDocument.Cities.Count);
        }
    }
}
